// pages/user/user.js
import { getInfo,formatDate,getStatisticsData} from '../../utils/api.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    avatar:'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0',
    level:'黄金代言人',
    arrive_num: 0,
    mobile: "",
    patient_num: 0,
    sale_money: 0,
    active:0,
    end_time:'',
    start_time:''
  },
  
  changeDate(e){
    if(e.currentTarget.id==this.data.active){
      return;
    }
    if(e.currentTarget.id==1){
      const date = new Date();
      let year = date.getFullYear();  
      let month = date.getMonth() + 1;
      let monthDate = new Date(year + '-' + month + '-01');
      this.setData({
        active:e.currentTarget.id,
        end_time:formatDate(date,1),
        start_time:formatDate(monthDate,1)
      })
    }else{
      this.setData({
        active:e.currentTarget.id,
        end_time:'',
        start_time:''
      })
    }    
    this.getData()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {  
    getInfo().then(res=>{
      this.setData({
        mobile: res.data.mobile,
        level:res.data.level_name,
        avatar:res.data.avatar_url?res.data.avatar_url:'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'
      })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    
  },
  getData(){
    let params={
      start_time:this.data.start_time,
      end_time:this.data.end_time
    }
    getStatisticsData(params).then(res=>{
      this.setData({
        arrive_num: res.data.arrive_num,
        patient_num: res.data.patient_num,
        sale_money: res.data.consume_money
      })
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getData()
  },
  toWithdraw(){
    wx.navigateTo({
      url: '/pages/withdraw/withdraw',
    })
  },
  toSuggestion(){
    wx.navigateTo({
      url: "/pages/suggestion/suggestion", 
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})